package org.epidroid.epidroid.androsoul.ui.contactlist;

import org.epidroid.epidroid.R;
import org.epidroid.epidroid.androsoul.engine.AndroSoul;
import org.epidroid.epidroid.androsoul.engine.contacts.ContactGroup;
import org.epidroid.epidroid.androsoul.engine.service.IAndroSoulService;
import org.epidroid.epidroid.androsoul.ui.AndroSoulActivity;

import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class NoContactForm {

	private AndroSoulActivity _activity = null;
	private IAndroSoulService _service = null;
	private LinearLayout _layout = null;
	private Button _buttonImportContacts = null;

	public NoContactForm(AndroSoulActivity activity, IAndroSoulService service) {
		_activity = activity;
		_service = service;
		_layout = (LinearLayout) _activity.findViewById(R.id.NoContactForm);
		_buttonImportContacts = (Button) _activity
				.findViewById(R.id.buttonImportContacts);

		_buttonImportContacts.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				NoContactForm.this._activity.importContacts();
			}
		});
	}

	public void hide() {
		_layout.setVisibility(View.GONE);
		Log.d(AndroSoul.LOGCAT, "HIDE");
	}

	public void show() {
		_layout.setVisibility(View.VISIBLE);
		Log.d(AndroSoul.LOGCAT, "VISIBLE");
	}

	/**
	 * Show the form that informs the user that he has the opportunity to import
	 * contacts from the phone when he does not have any contact.
	 */
	public void update() {

		Boolean hasContacts = false;
		for (ContactGroup g : _service.getGroupsList()) {
			if (!g.getList().isEmpty()) {
				hasContacts = true;
				break;
			}
		}

		if (hasContacts)
			hide();
		else
			show();
	}

}
